package com.zshan.clinic.web.controller.config;


import com.zshan.clinic.admin.entity.dto.ClinicEmpScheduleDTO;
import com.zshan.clinic.admin.entity.dto.CopyScheduleDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.config.EmpScheduleService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.ClinicDoctorDict;
import com.zshan.clinic.database.model.ClinicEmpShift;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 充值档位
 */
@RestController
@RequestMapping("/emp/schedule")
public class EmpScheduleController extends AbstractController {


    @Autowired
    private EmpScheduleService empScheduleService;

    /**
     * 工作台：获取我的排班
     * @return
     */
    @GetMapping("/getMySchedule")
    public RestResp<List<ClinicEmpScheduleVo>> getMySchedule() {
        UserInfo userInfo = getUserInfo();
        List<ClinicEmpScheduleVo> scheduleList = empScheduleService.getMySchedule(userInfo.getClinicId(), userInfo.getEmpId());
        return RestResp.SUCCESS(scheduleList);
    }

    /**
     * 查询员工排班列表
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param empName 员工姓名
     * @param deptId 所属部门
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getEmpScheduleList")
    public RestResp<PageInfo<ClinicEmpSchedulePageVo>> getEmpScheduleList(@RequestParam(name = "startDate", required = false) LocalDate startDate,
                                                                          @RequestParam(name = "endDate", required = false) LocalDate endDate,
                                                                          @RequestParam(name = "empName", required = false) String empName,
                                                                          @RequestParam(name = "deptId", required = false) Long deptId,
                                                                          @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                          @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicEmpSchedulePageVo> pageInfo = empScheduleService.getEmpScheduleList(userInfo.getClinicId(), startDate,endDate,empName,deptId, pageNo, pageSize);
        return RestResp.SUCCESS(pageInfo);
    }

    /**
     * 维护排班
     * @param clinicEmpScheduleDTO
     * @return
     */
    @PostMapping("/saveSchedule")
    public RestResp<CancelRegisterVo> saveSchedule(@RequestBody ClinicEmpScheduleDTO clinicEmpScheduleDTO) {
        UserInfo userInfo = getUserInfo();
        clinicEmpScheduleDTO.setCreateUserId(userInfo.getEmpId());
        clinicEmpScheduleDTO.setCreateBy(userInfo.getEmpName());
        clinicEmpScheduleDTO.setClinicId(userInfo.getClinicId());
        CancelRegisterVo cancelRegisterVo = empScheduleService.saveSchedule(clinicEmpScheduleDTO);
        return RestResp.SUCCESS(cancelRegisterVo);
    }

    /**
     * 复制排班
     * @return
     */
    @PostMapping("/copySchedule")
    public RestResp<CancelRegisterVo> copySchedule(@RequestBody CopyScheduleDTO copyScheduleDTO) {
        UserInfo userInfo = getUserInfo();
        copyScheduleDTO.setCreateUserId(userInfo.getEmpId());
        copyScheduleDTO.setCreateBy(userInfo.getEmpName());
        copyScheduleDTO.setClinicId(userInfo.getClinicId());
        CancelRegisterVo cancelRegisterVo = empScheduleService.copySchedule(copyScheduleDTO);
        return RestResp.SUCCESS(cancelRegisterVo);
    }


    /**
     * 查询班次列表
     * @param
     * @return
     */
    @GetMapping("/getShiftList")
    public RestResp<PageInfo<ClinicEmpShiftVo>> getShiftList(@RequestParam(name = "shiftName", required = false) String shiftName,
                                                             @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicEmpShiftVo> shiftList = empScheduleService.getShiftList(userInfo.getClinicId(), shiftName, pageNo, pageSize);
        return RestResp.SUCCESS(shiftList);
    }

    /**
     * 添加班次
     * @return
     */
    @PostMapping("/addShift")
    public RestResp<Void> addShift(@RequestBody ClinicEmpShift shift) {
        UserInfo userInfo = getUserInfo();
        shift.setCreateUserId(userInfo.getEmpId());
        shift.setCreateBy(userInfo.getEmpName());
        shift.setClinicId(userInfo.getClinicId());
        empScheduleService.addShift(shift);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑班次
     * @return
     */
    @PostMapping("/updateShift")
    public RestResp<CancelRegisterVo> updateShift(@RequestBody ClinicEmpShift shift) {
        UserInfo userInfo = getUserInfo();
        shift.setUpdateUserId(userInfo.getEmpId());
        shift.setUpdateBy(userInfo.getEmpName());
        shift.setClinicId(userInfo.getClinicId());
        CancelRegisterVo cancelRegisterVo = empScheduleService.updateShift(shift);
        return RestResp.SUCCESS(cancelRegisterVo);
    }

    /**
     * 删除班次
     * @return
     */
    @PostMapping("/deleteShift")
    public RestResp<CancelRegisterVo> deleteShift(@RequestBody ClinicEmpShift shift) {
        UserInfo userInfo = getUserInfo();
        shift.setUpdateUserId(userInfo.getEmpId());
        shift.setUpdateBy(userInfo.getEmpName());
        shift.setClinicId(userInfo.getClinicId());
        CancelRegisterVo cancelRegisterVo = empScheduleService.deleteShift(shift);
        return RestResp.SUCCESS(cancelRegisterVo);
    }

    /**
     * 启用班次
     * @return
     */
    @PostMapping("/enableShift")
    public RestResp<Void> enableShift(@RequestBody ClinicEmpShift shift) {
        UserInfo userInfo = getUserInfo();
        shift.setUpdateUserId(userInfo.getEmpId());
        shift.setUpdateBy(userInfo.getEmpName());
        shift.setClinicId(userInfo.getClinicId());
        empScheduleService.enableShift(shift);
        return RestResp.SUCCESS();
    }

    /**
     * 停用班次
     * @return
     */
    @PostMapping("/disableShift")
    public RestResp<Void> disableShift(@RequestBody ClinicEmpShift shift) {
        UserInfo userInfo = getUserInfo();
        shift.setUpdateUserId(userInfo.getEmpId());
        shift.setUpdateBy(userInfo.getEmpName());
        shift.setClinicId(userInfo.getClinicId());
        empScheduleService.disableShift(shift);
        return RestResp.SUCCESS();
    }

    /**
     * 查询挂号常量列表
     * @param
     * @return
     */
    @GetMapping("/getDoctorDict")
    public RestResp<ClinicDoctorDictVo> getDoctorDict() {
        UserInfo userInfo = getUserInfo();
        ClinicDoctorDictVo doctorDictVo = empScheduleService.getDoctorDict(userInfo.getClinicId());
        return RestResp.SUCCESS(doctorDictVo);
    }

    /**
     * 保存挂号常量
     * @return
     */
    @PostMapping("/saveDict")
    public RestResp<Void> saveDict(@RequestBody ClinicDoctorDict dict) {
        UserInfo userInfo = getUserInfo();
        dict.setCreateUserId(userInfo.getEmpId());
        dict.setCreateBy(userInfo.getEmpName());
        dict.setClinicId(userInfo.getClinicId());
        empScheduleService.saveDict(dict);
        return RestResp.SUCCESS();
    }
}
